<?php
/**
 * 内容/话题关联wiki列表
 */

use Biz\model\wiki\ObjRelateWiki as mObjRelateWiki;
use Biz\service\wiki\ObjRelateWiki as sObjRelateWiki;
use Biz\tools\Location;
use Doraemon\tools\ActionLog;
use Doraemon\tools\BlockType;
use Doraemon\tools\HhzClient;
use Doraemon\tools\IdParse;

class RelatedListAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_NOTHING;
    const IS_LOG_EXPOSE = 1;

    public function main()
    {
        $uid = $this->uid;
        $objId = $this->getRequest()->getQuery('obj_id', '');
        $objType = $this->getRequest()->getQuery('obj_type', 0);
        $page = $this->getRequest()->getQuery('page', 0);
        $params = $this->getRequest()->getQuery('params', '');
        $params = $params ? json_decode($params, true) : [];
        $result = ['is_over' => 1, 'list' => []];
        if ($objId && $objType) {
            if ($objType == mObjRelateWiki::OBJ_TYPE_TOPIC) {
                $result = $this->getTopicWiki($objId, $page, $this->uid);
            } else if ($params) {
                $noteId = $params['note_id'] ?? '';
                $imgIds = $params['img_ids'] ?? [];
                if ($noteId) {
                    $result = sObjRelateWiki::getRelateWikiList($noteId, mObjRelateWiki::OBJ_TYPE_NOTE, [], $uid);
                    $result['list'] = sObjRelateWiki::formatRelatedWikiStatSign($objId, $result['list'], ['note_id' => $noteId]);
                    $result['is_over'] = 1;
                } else if ($imgIds) {
                    $result = sObjRelateWiki::getRelateWikiList($objId, $objType, $imgIds, $uid);
                    $result['list'] = sObjRelateWiki::formatRelatedWikiStatSign($objId, $result['list'], ['img_ids' => $imgIds]);
                    $result['is_over'] = 1;
                }
            } else {
                $result = sObjRelateWiki::getRelateWikiList($objId, $objType, [], $uid);
                $result['list'] = sObjRelateWiki::formatRelatedWikiStatSign($objId, $result['list']);
                /*$location = new Location($uid);
                $showStore = $location->isShowStore();
                $objUid = IdParse::getUid($objId);
                $isOwner = ($uid == $objUid && $uid > 0) ? true : false;
                foreach ($result['list'] as $key => $row) {
                    if (!$isOwner && !$showStore && $row['store_id'] > 0) {//门店的商品，只有在北京，或则当前内容是自己的，则展示
                        unset($result['list'][$key]);
                    }
                }*/ //门店下线
                $result['is_over'] = 1;
            }
        }
        $result['list'] = $this->formatList($result['list']);
        $this->setSuccess($result);
    }

    private function getTopicWiki($objId, $page, $uid)
    {
        $result = sObjRelateWiki::getTopicRelated($objId, $page, $uid);
        ActionLog::log($uid, ['obj_id' => $objId], ActionLog::TOPIC_DETAIL_WIKI_LIST);
        return $result;
    }

    private function formatList($list)
    {
        $result = [];

        foreach ($list as $row) {
            $wikiInfo = $row;
            unset($wikiInfo['statSign']);
            $result[] = [
                'type' => BlockType::TYPE_WIKI,
                'wiki' => $wikiInfo,
                'statSign' => $row['statSign'] ?? ''
            ];
        }

        return $result;
    }
}